package com.sx.widget.view;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class DividerMargin extends RecyclerView.ItemDecoration {
    public static final int DIVIDER_ONE_TOP = -1;//只有顶部
    public static final int DIVIDER_TOP = 1;
    public static final int DIVIDER_BOTTOM = 2;//只有底部
    public static final int DIVIDER_Left_RIGHT = 3;//左右
    public static final int DIVIDER_TOP_Left_RIGHT = 4;//顶部+左右
    public static final int DIVIDER_BOTTOM_LEFT_RIGHT = 5;//底部+左右
    public static final int DIVIDER_ONE_BOTTOM = 7;//只有底部
    public static final int DIVIDER_RIGHT = 6;//右边
    public static final int DIVIDER_SPACE = 8;
    private int margin_space = 0;
    private int margin_top = 0;
    private int margin_bottom = 0;
    private int margin_left = 0;
    private int margin_right = 0;
    private int mode = 0;

    public DividerMargin(int mode, int margin) {
        this.mode = mode;
        if (mode == DIVIDER_TOP || mode == DIVIDER_ONE_TOP) {
            this.margin_top = margin;
        }
        if (mode == DIVIDER_BOTTOM || mode == DIVIDER_ONE_BOTTOM) {
            this.margin_bottom = margin;
        }
        if (mode == DIVIDER_Left_RIGHT) {
            this.margin_left = margin;
            this.margin_right = margin;
        }
        if (mode == DIVIDER_RIGHT) {
            this.margin_right = margin;
        }
        if (mode == DIVIDER_SPACE) {
            this.margin_space = margin;
        }

    }

    public DividerMargin(int mode, int marginTb, int marginL, int marginR) {
        if (mode == DIVIDER_TOP_Left_RIGHT) {
            this.margin_top = marginTb;
            this.margin_left = marginL;
            this.margin_right = marginR;
        }
        if (mode == DIVIDER_BOTTOM_LEFT_RIGHT) {
            this.margin_bottom = marginTb;
            this.margin_left = marginL;
            this.margin_right = marginR;
        }

    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (mode == DIVIDER_ONE_TOP || mode == DIVIDER_ONE_BOTTOM||mode== DIVIDER_SPACE) {
            if (layoutManager instanceof LinearLayoutManager) {
                int position = parent.getChildAdapterPosition(view);
                if (mode == DIVIDER_ONE_TOP) {
                    if (position == 0) {
                        outRect.top = margin_top;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = margin_bottom;
                    } else {
                        outRect.top = 0;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = margin_bottom;
                    }
                } else {
                    if (position == layoutManager.getItemCount() - 1) {
                        outRect.top = margin_top;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = margin_bottom;
                    } else {
                        outRect.top = margin_top;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = 0;
                    }
                }

            } else if (layoutManager instanceof StaggeredGridLayoutManager || layoutManager instanceof GridLayoutManager) {
                int spanCount = 0;
                if (layoutManager instanceof StaggeredGridLayoutManager) {
                    StaggeredGridLayoutManager layoutManager1 = (StaggeredGridLayoutManager) layoutManager;
                    spanCount = layoutManager1.getSpanCount();
                } else if (layoutManager instanceof GridLayoutManager) {
                    GridLayoutManager layoutManager1 = (GridLayoutManager) layoutManager;
                    spanCount = layoutManager1.getSpanCount();
                }
                int position = parent.getChildAdapterPosition(view);
                if (mode == DIVIDER_SPACE) {
                    int space = margin_space / 2;
                    position += 1;
                    if (position % spanCount == 0) {
                        outRect.left = space;
                        outRect.right = 0;
                        outRect.top = 0;
                        outRect.bottom = 0;
                    } else if (position % spanCount == 1) {
                        outRect.right = space;
                        outRect.left = 0;
                        outRect.top = 0;
                        outRect.bottom = 0;
                    } else {
                        outRect.left = space;
                        outRect.right = space;
                        outRect.top = 0;
                        outRect.bottom = 0;
                    }
                } else {
                    if (position < spanCount) {
                        outRect.top = margin_top;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = margin_bottom;
                    } else {
                        outRect.top = 0;
                        outRect.left = margin_left;
                        outRect.right = margin_right;
                        outRect.bottom = margin_bottom;
                    }
                }

            }

        } else {
            outRect.top = margin_top;
            outRect.left = margin_left;
            outRect.right = margin_right;
            outRect.bottom = margin_bottom;
        }

    }
}
